// utils/api.js
const { request } = require('./request')

/**
 * 记忆胶囊接口
 * @typedef {Object} MemoryCapsule
 * @property {string} id
 * @property {string} title
 * @property {string} description
 * @property {string} [coverImage]
 * @property {string} template
 * @property {boolean} isPrivate
 * @property {boolean} allowShare
 * @property {string} userId
 * @property {string} createTime
 * @property {string} updateTime
 * @property {string} lastActiveTime
 * @property {number} entryCount
 * @property {number} textCount
 * @property {number} imageCount
 * @property {number} audioCount
 * @property {boolean} isFavorite
 * @property {number} shareCount
 * @property {number} viewCount
 */

/**
 * 记忆条目接口
 * @typedef {Object} MemoryEntry
 * @property {string} id
 * @property {string} capsuleId
 * @property {'text'|'image'|'audio'} type
 * @property {string} content
 * @property {string} [imageUrl]
 * @property {string} [audioUrl]
 * @property {number} [audioDuration]
 * @property {string} createTime
 * @property {string} updateTime
 */

/**
 * 分页参数接口
 * @typedef {Object} PageParams
 * @property {number} page
 * @property {number} size
 * @property {string} [keyword]
 * @property {string} [template]
 * @property {'createTime'|'updateTime'|'lastActiveTime'} [sortBy]
 * @property {'asc'|'desc'} [sortOrder]
 */

/**
 * 分页响应接口
 * @typedef {Object} PageResponse
 * @property {Array} list
 * @property {number} total
 * @property {number} page
 * @property {number} size
 * @property {number} totalPages
 * @property {boolean} hasNext
 * @property {boolean} hasPrev
 */

/**
 * 统计数据接口
 * @typedef {Object} UserStats
 * @property {number} capsuleCount
 * @property {number} entryCount
 * @property {number} activeDays
 * @property {number} favoriteCount
 * @property {number} shareCount
 * @property {number} totalWords
 * @property {number} totalImages
 * @property {number} totalAudios
 */

/**
 * 胶囊统计接口
 * @typedef {Object} CapsuleStats
 * @property {number} entryCount
 * @property {number} textCount
 * @property {number} imageCount
 * @property {number} audioCount
 * @property {number} totalWords
 * @property {number} viewCount
 * @property {number} shareCount
 * @property {number} favoriteCount
 */

/**
 * 文件上传响应接口
 * @typedef {Object} UploadResponse
 * @property {string} url
 * @property {string} filename
 * @property {number} size
 * @property {string} mimeType
 */

/**
 * 导出数据接口
 * @typedef {Object} ExportData
 * @property {MemoryCapsule} capsule
 * @property {MemoryEntry[]} entries
 * @property {string} exportTime
 * @property {'json'|'pdf'|'word'} format
 */

// 认证相关API
const authApi = {
  // 微信登录
  wxLogin: (data) => 
    request.post('/api/auth/wx-login', data),
  
  // 刷新token
  refreshToken: (refreshToken) => 
    request.post('/api/auth/refresh', { refreshToken }),
  
  // 验证token
  verifyToken: () => 
    request.get('/api/auth/verify'),
  
  // 退出登录
  logout: () => 
    request.post('/api/auth/logout')
}

// 用户相关API
const userApi = {
  // 获取用户信息
  getProfile: () => 
    request.get('/api/user/profile'),
  
  // 更新用户信息
  updateProfile: (data) => 
    request.put('/api/user/profile', data),
  
  // 绑定手机号
  bindPhone: (code) => 
    request.post('/api/user/bind-phone', { code }),
  
  // 获取用户统计
  getStats: () => 
    request.get('/api/user/stats'),
  
  // 获取用户设置
  getSettings: () => 
    request.get('/api/user/settings'),
  
  // 更新用户设置
  updateSettings: (settings) => 
    request.put('/api/user/settings', settings)
}

// 记忆胶囊相关API
const capsuleApi = {
  // 获取胶囊列表
  getList: (params) => 
    request.get('/api/capsules', params),
  
  // 获取胶囊详情
  getDetail: (id) => 
    request.get(`/api/capsules/${id}`),
  
  // 创建胶囊
  create: (data) => 
    request.post('/api/capsules', data),
  
  // 更新胶囊
  update: (id, data) => 
    request.put(`/api/capsules/${id}`, data),
  
  // 删除胶囊
  delete: (id) => 
    request.delete(`/api/capsules/${id}`),
  
  // 收藏/取消收藏胶囊
  toggleFavorite: (id) => 
    request.post(`/api/capsules/${id}/favorite`),
  
  // 分享胶囊
  share: (id) => 
    request.post(`/api/capsules/${id}/share`),
  
  // 获取胶囊统计
  getStats: (id) => 
    request.get(`/api/capsules/${id}/stats`),
  
  // 导出胶囊
  export: (id, format) => 
    request.post(`/api/capsules/${id}/export`, { format }),
  
  // 搜索胶囊
  search: (params) => 
    request.get('/api/capsules/search', params)
}

// 记忆条目相关API
const entryApi = {
  // 获取条目列表
  getList: (capsuleId, params) => 
    request.get(`/api/capsules/${capsuleId}/entries`, params),
  
  // 获取条目详情
  getDetail: (capsuleId, entryId) => 
    request.get(`/api/capsules/${capsuleId}/entries/${entryId}`),
  
  // 创建文字条目
  createText: (capsuleId, content) => 
    request.post(`/api/capsules/${capsuleId}/entries`, {
      type: 'text',
      content
    }),
  
  // 创建图片条目
  createImage: (capsuleId, imageUrl, content) => 
    request.post(`/api/capsules/${capsuleId}/entries`, {
      type: 'image',
      imageUrl,
      content: content || ''
    }),
  
  // 创建语音条目
  createAudio: (capsuleId, audioUrl, duration, content) => 
    request.post(`/api/capsules/${capsuleId}/entries`, {
      type: 'audio',
      audioUrl,
      audioDuration: duration,
      content: content || ''
    }),
  
  // 更新条目
  update: (capsuleId, entryId, data) => 
    request.put(`/api/capsules/${capsuleId}/entries/${entryId}`, data),
  
  // 删除条目
  delete: (capsuleId, entryId) => 
    request.delete(`/api/capsules/${capsuleId}/entries/${entryId}`),
  
  // 搜索条目
  search: (capsuleId, params) => 
    request.get(`/api/capsules/${capsuleId}/entries/search`, params)
}

// 文件上传相关API
const uploadApi = {
  // 上传图片
  uploadImage: (filePath, capsuleId) => 
    request.uploadFile({
      url: '/api/upload/image',
      filePath,
      name: 'file',
      formData: capsuleId ? { capsuleId } : undefined
    }),
  
  // 上传语音
  uploadAudio: (filePath, capsuleId) => 
    request.uploadFile({
      url: '/api/upload/audio',
      filePath,
      name: 'file',
      formData: capsuleId ? { capsuleId } : undefined
    }),
  
  // 上传头像
  uploadAvatar: (filePath) => 
    request.uploadFile({
      url: '/api/upload/avatar',
      filePath,
      name: 'file'
    }),
  
  // 上传胶囊封面
  uploadCover: (filePath) => 
    request.uploadFile({
      url: '/api/upload/cover',
      filePath,
      name: 'file'
    }),
  
  // 删除文件
  deleteFile: (url) => 
    request.delete('/api/upload/file', { data: { url } })
}

// 系统相关API
const systemApi = {
  // 获取系统配置
  getConfig: () => 
    request.get('/api/system/config'),
  
  // 获取胶囊模板
  getTemplates: () => 
    request.get('/api/system/templates'),
  
  // 检查版本更新
  checkUpdate: () => 
    request.get('/api/system/check-update'),
  
  // 反馈问题
  feedback: (data) => 
    request.post('/api/system/feedback', data),
  
  // 获取帮助文档
  getHelp: () => 
    request.get('/api/system/help')
}

// VIP相关API
const vipApi = {
  // 获取VIP信息
  getInfo: () => 
    request.get('/api/vip/info'),
  
  // 获取VIP套餐
  getPackages: () => 
    request.get('/api/vip/packages'),
  
  // 创建支付订单
  createOrder: (packageId) => 
    request.post('/api/vip/order', { packageId }),
  
  // 查询订单状态
  getOrderStatus: (orderId) => 
    request.get(`/api/vip/order/${orderId}`)
}

// 分享相关API
const shareApi = {
  // 获取分享信息
  getShareInfo: (capsuleId) => 
    request.get(`/api/share/capsule/${capsuleId}`),
  
  // 记录分享
  recordShare: (capsuleId, platform) => 
    request.post('/api/share/record', { capsuleId, platform }),
  
  // 获取分享统计
  getShareStats: (capsuleId) => 
    request.get(`/api/share/stats/${capsuleId}`)
}

// 搜索相关API
const searchApi = {
  // 全局搜索
  global: (params) => 
    request.get('/api/search', params),
  
  // 搜索建议
  suggestions: (keyword) => 
    request.get('/api/search/suggestions', { keyword }),
  
  // 搜索历史
  getHistory: () => 
    request.get('/api/search/history'),
  
  // 清除搜索历史
  clearHistory: () => 
    request.delete('/api/search/history'),
  
  // 热门搜索
  getHotKeywords: () => 
    request.get('/api/search/hot')
}

// 导出所有API
module.exports = {
  authApi,
  userApi,
  capsuleApi,
  entryApi,
  uploadApi,
  systemApi,
  vipApi,
  shareApi,
  searchApi
}